/*
 * Copyright 2017 Baiyyy
 * @(#)ParamSettingService.java
 */

package com.byyy.ccts.modules.sys.service.impl;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.byyy.ccts.core.common.service.impl.CommonServiceImpl;
import com.byyy.ccts.core.query.data.PageImpl;
import com.byyy.ccts.core.query.data.Pageable;
import com.byyy.ccts.core.query.data.Queryable;
import com.byyy.ccts.core.query.parse.QueryToWrapper;
import com.byyy.ccts.core.utils.CacheUtils;
import com.byyy.ccts.core.utils.StringUtils;
import com.byyy.ccts.modules.cc.entity.ccts.TADevice;
import com.byyy.ccts.modules.sys.entity.TZParam;
import com.byyy.ccts.modules.sys.mapper.ParamMapper;
import com.byyy.ccts.modules.sys.service.IParamService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service("paramService")
public class ParamService extends CommonServiceImpl<ParamMapper, TZParam> implements IParamService {

    /**
     * 更新参数可用状态
     * @param ids
     * @param state
     */
    @Override
    public void updateState(List<String> ids, String state) {
        this.baseMapper.updateState(ids, state);
    }

    /**
     * 根据参数编码返回参数值
     * @param code
     * @return
     */
    @Override
    public String getValueByCode(String code) {
        String value = (String) CacheUtils.get(CacheUtils.SYS_CACHE, code);
        if (StringUtils.isEmpty(value)) {
            TZParam paramSetting = new TZParam();
            paramSetting.setCode(code);
            value = this.baseMapper.selectOne(paramSetting).getValue();
            CacheUtils.put(CacheUtils.SYS_CACHE, code, value);
        }

        return value;
    }

    /**
     * 将参数值转换为Float类型再返回
     * @param code
     * @return
     */
    @Override
    public float getFloatValueByCode(String code) {
        Float ret = (Float) CacheUtils.get(CacheUtils.SYS_CACHE, code);
        if (ret == null) {
            TZParam paramSetting = new TZParam();
            paramSetting.setCode(code);
            try {
                ret = Float.valueOf(this.baseMapper.selectOne(paramSetting).getValue());
            }
            catch (Exception e) {
                ret = 0f;
            }
            CacheUtils.put(CacheUtils.SYS_CACHE, code, ret);
        }

        return ret;
    }

    /**
     * 根据参数编码返回参数
     * @param code
     * @return
     */
    @Override
    public TZParam getParamByCode(String code) {
        TZParam paramSetting = new TZParam();
        paramSetting.setCode(code);
        return this.baseMapper.selectOne(paramSetting);
    }

    /**
     * 参数列表
     * @param queryable
     * @param wrapper
     * @return
     */
    @Override
    public com.byyy.ccts.core.query.data.Page<TZParam> paramList(Queryable queryable, Wrapper<TZParam> wrapper) {
        QueryToWrapper<TZParam> queryToWrapper = new QueryToWrapper<>();
        queryToWrapper.parseCondition(wrapper, queryable);
        // 排序问题
        queryToWrapper.parseSort(wrapper, queryable);
        Pageable pageable = queryable.getPageable();
        Page<TZParam> page = new Page<TZParam>(pageable.getPageNumber(), pageable.getPageSize());
        Page<TZParam> content = paramListPage(page, wrapper);
        return new PageImpl<TZParam>(content.getRecords(), queryable.getPageable(), content.getTotal());
    }

    /**
     * 参数列表
     * @param page
     * @param wrapper
     * @return
     */
    @Override
    public Page<TZParam> paramListPage(Page<TZParam> page, Wrapper<TZParam> wrapper) {
        page.setRecords(baseMapper.paramListPage(page, wrapper));
        return page;
    }

}
